Button focus problem

I have a script that behaves as expected:

One apply() button is used, and it gets default focus via set() call, so that ENTER invokes the callback function associated with the apply() button.

However in a couple other scripts, the layout is similar but the set() call does not work: the focus is not given to the apply() button.

From what I can see, the only difference is the problematic scripts use a (read-only) text() DBE. Is this really the issue?

Is there no way to give focus to an apply() button when the dialog contains a text() DBE?

 


strathglass - Fri Apr 22 12:57:43 EDT 2016

Re: Button focus problem
O.Wilkop - Tue Apr 26 08:47:40 EDT 2016

void onApply(DB x){
        print "got focus\n";
}

DB main = centered("");
DBE textbox = text(main , "textfield", "some value", 20, true);
DBE applybutton = apply(main , onApply);


realize main;
setFocus(applybutton);
show main;

 

This works for me, make sure to call setFocus() after realize()

Re: Button focus problem
strathglass - Tue Apr 26 09:00:06 EDT 2016

Thank you O.W.!

You helped me realize what I was missing (no pun intended): the realize() call.

However I am not sure why realize() is required to make the setFocus() work, I thought the later show() was all I needed.

I guess the help could be clearer...if you know why, please share.

Re: Button focus problem
O.Wilkop - Tue Apr 26 09:59:23 EDT 2016

From my understanding the realize() function creates the internal structure of the dialog box without activating it.

If you call show() without realize() it will call realize() for you internally.

 

Before calling realize() no element exists and therefore can't have focus.

Right after calling realize() one element will have the focus, but it is rather random to you which one.

Between realize() and show() you can change the focus with setFocus()

Finally, show() will activate the window and display it correctly.

 

If you call setFocus() before realize() it will just get overwritten/ignored once realize gets called (either by your code or by the show() function).

Re: Button focus problem
strathglass - Tue Apr 26 12:00:12 EDT 2016

Makes sense, thanks!